//--------------------------------------------------------------------------------
// This file is part of the Windows Workflow Foundation Sample Code
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// This source code is intended only as a supplement to Microsoft
// Development Tools and/or on-line documentation.  See these other
// materials for detailed information regarding Microsoft code samples.
// 
// THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//--------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Compiler;

namespace Microsoft.Samples.Workflow.UI
{
    class InteractionActivityValidator : InteractionBaseActivityValidator
    {
        public override ValidationErrorCollection Validate(ValidationManager manager, object obj)
        {
            ValidationErrorCollection errors = base.Validate(manager, obj);

            InteractionActivity interactionActivity = obj as InteractionActivity;
            if (interactionActivity == null)
            {
                throw new ArgumentException(SR.GetString(SR.Error_UnexpectedArgumentType, typeof(InteractionActivity).FullName), "obj");
            }

            ValidateChildren(errors, interactionActivity);

            return errors;
        }

        static void ValidateChildren(ValidationErrorCollection errors, InteractionActivity interactionActivity)
        {
            CompositeActivity compositeActivity = interactionActivity as CompositeActivity;
            Queue<CompositeActivity> compositeActivities = new Queue<CompositeActivity>();
            compositeActivities.Enqueue(compositeActivity);
            while (compositeActivities.Count > 0)
            {
                compositeActivity = compositeActivities.Dequeue();
                foreach (Activity child in compositeActivity.EnabledActivities)
                {
                    if (Helpers.IsOOBStateMachineActivity(child.GetType()))
                    {
                        errors.Add(new ValidationError(SR.GetString(SR.Error_NoStatemachineActivity, child.QualifiedName), ErrorNumbers.Error_NoStatemachineActivity));
                    }

                    if (Helpers.IsOOBWebServiceActivity(child.GetType()))
                    {
                        errors.Add(new ValidationError(SR.GetString(SR.Error_NoWebServiceActivity, child.QualifiedName), ErrorNumbers.Error_NoWebServiceActivity));
                    }

                    if (child is InteractionBaseActivity || child is Navigator)
                    {
                        errors.Add(new ValidationError(SR.GetString(SR.Error_ChildCantBeInteraction, child.QualifiedName), ErrorNumbers.Error_ChildCantBeInteraction));
                    }

                    if (interactionActivity.ReceiveInput && Helpers.IsOOBEventTypeActivity(child.GetType()))
                    {
                        errors.Add(new ValidationError(SR.GetString(SR.Error_NoIEventActivityInInteractionActivity, child.QualifiedName), ErrorNumbers.Error_NoIEventActivityInInteractionActivity));
                    }

                    if (child is CompositeActivity)
                    {
                        compositeActivities.Enqueue(child as CompositeActivity);
                    }
                }
            }

            return;
        }
    }
}
